<template>
  <div class="multiple-form-inp-example-container">
    <div class="form-container">
      <div class="form-content-title">基本信息</div>
      <BasicForm @register="registerForm" ref="formRef" name="InspectionPlanMainPjzForm" />
      <!-- 子表单区域 -->
       <div class="form-content-title">元素列表</div>
      <a-tabs v-model:activeKey="activeKey" animated @change="handleChangeTabs">
        <a-tab-pane tab="明细表1" key="detailColumnsone" :forceRender="true">
          <JVxeTable
            keep-source
            resizable
            ref="detailColumnsone"
            :loading="detailColumnsoneTable.loading"
            :columns="detailColumnsoneTable.columns"
            :dataSource="detailColumnsoneTable.dataSource"
            :height="340"
            :rowNumber="true"
            :rowSelection="true"
            :disabled="formDisabled"
            :toolbar="true"
          />
        </a-tab-pane>
        <a-tab-pane tab="明细表2" key="detailColumnstwo" :forceRender="true">
          <JVxeTable
            keep-source
            resizable
            ref="detailColumnstwo"
            :loading="detailColumnstwoTable.loading"
            :columns="detailColumnstwoTable.columns"
            :dataSource="detailColumnstwoTable.dataSource"
            :height="340"
            :rowNumber="true"
            :rowSelection="true"
            :disabled="formDisabled"
            :toolbar="true"
          />
        </a-tab-pane>
      </a-tabs>
    </div>
    <div class="btn-container cyj-group-divider">
      <!-- <a-button style="margin-right: 10px" type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button> -->
      <a-button preIcon="ant-design:sync-outlined" @click="onReset">重置</a-button>
      <a-button type="primary" preIcon="ant-design:save-filled" @click="handleSubmit">保存</a-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, computed, unref, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { JVxeTable } from '/@/components/base/JVxeTable';
  import { useJvxeMethod } from '/@/hooks/system/useJvxeMethods.ts';
  import { formSchema, DetailColumnsone, DetailColumnstwo } from './demonstration.data';
  import { VALIDATE_FAILED } from '/@/utils/common/vxeUtils';
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const formDisabled = ref(false);
  const refKeys = ref(['detailColumnsone', 'detailColumnstwo']);
  const activeKey = ref('detailColumnsone');
  const detailColumnsone = ref();
  const detailColumnstwo = ref();
  const tableRefs = { detailColumnsone, detailColumnstwo };
  const detailColumnsoneTable = reactive({
    loading: false,
    dataSource: [],
    columns: DetailColumnsone,
  });
  const detailColumnstwoTable = reactive({
    loading: false,
    dataSource: [],
    columns: DetailColumnstwo,
  });
  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 12 },
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await reset();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    formDisabled.value = !data?.showFooter;
    if (unref(isUpdate)) {
      await setFieldsValue({
        ...data.record,
      });
    }
    // 隐藏底部时禁用整个表单
    setProps({ disabled: !data?.showFooter });
  });
  //方法配置
  const {handleChangeTabs, handleSubmit, requestSubTableData, formRef}= useJvxeMethod(
    requestAddOrEdit,
    classifyIntoFormData,
    tableRefs,
    activeKey,
    refKeys
  );

  //设置标题
  const title = '录入类弹窗表单带子表示例';

  async function reset() {
    await resetFields();
    activeKey.value = 'detailColumnsone';
    detailColumnsoneTable.dataSource = [];
    detailColumnstwoTable.dataSource = [];
  }
  function classifyIntoFormData(allValues) {
    let main = Object.assign({}, allValues.formValue);
    return {
      ...main, // 展开
    };
  }
  //表单提交事件
  async function requestAddOrEdit(values) {
    try {
      setModalProps({ confirmLoading: true });
      //关闭弹窗
      closeModal();
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  // 重置表单
  async function onReset() {
    await resetFields();
  }
</script>

<style lang="less" scoped>
  @import '/@/design/reference/form.less';
  .multiple-form-inp-example-container {
    background: #fff;
    box-sizing: border-box;
    height: calc(100vh - 132px);
    border-radius: 8px;
    .form-container {
      width: 100%;
      height: calc(100% - 64px);
      overflow: auto;
      padding: 16px;
    }
    .btn-container {
      width: 100%;
      // height: 50px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      padding: 16px;
    }
  }
</style>
